package cn.zhz.service.user;

import cn.zhz.dao.user.UserMapper;
import cn.zhz.entity.User;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;

	@Override
	public boolean add(User user) {
		boolean result = false;
		try {
			if (userMapper.add(user) == 1) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public User login(String userCode, String userPassword) {
		User user = null;
		try {
			user = userMapper.getLoginUser(userCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
		List<User> userList = null;
		try {
			userList = userMapper.getUserList(queryUserName, queryUserRole, (currentPageNo - 1) * pageSize, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userList;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public int getUserCount() {
		int result = 0;
		try {
			result = userMapper.getUserCount();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public User selectUserCodeExist(String userCode) {
		User user = null;
		try {
			user = userMapper.getLoginUser(userCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean deleteUserById(Integer delId) {
		boolean result = false;
		try {
			User user = userMapper.getUserById(delId);
			if (!StringUtils.isNullOrEmpty(user.getIdPicPath())
					|| !StringUtils.isNullOrEmpty(user.getWorkPicPath())) {
				user.setIdPicPath("");
				user.setWorkPicPath("");
			}
			if (userMapper.deleteUserById(delId) == 1) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public User getUserById(Integer id) {
		User user = null;
		try {
			user = userMapper.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean modify(User user) {
		boolean result = false;
		try {
			if (userMapper.modify(user) == 1) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean updatePwd(int id, String pwd) {
		boolean result = false;
		try {
			if (userMapper.updatePwd(id, pwd) == 1) {
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

